package com.meng.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * PublishNettyServer 注解解析类
 *
 * @author MENG
 * @version 2018/3/20
 * @see
 */
@Component
public class PublishNettyServerAnalysis implements BeanPostProcessor
{

    Logger logger = LoggerFactory.getLogger(PublishNettyServerAnalysis.class);

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
    {
        if(bean.getClass().isAnnotationPresent(PublishNettyServer.class))
        {
            try
            {
                Method start = bean.getClass().getMethod("start");

                start.invoke(bean);
            }
            catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e)
            {
                logger.error("Netty server start failed -> {}",beanName,e);
            }
        }

        return bean;
    }
}
